﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Koala.Pro.Models;
using Koala.Pro.Services;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace Koala.Pro.ViewModels
{
    public abstract partial class BaseViewModel : ObservableObject, INavigationAware
    {
        private readonly INavigationService navigationService;

        private readonly IWindowsProviderService windowsService;

        [ObservableProperty]
        private string title = "";

        [ObservableProperty]
        private CommonResult result = new CommonResult(success: false);

        public BaseViewModel()
        {
            navigationService = App.GetService<INavigationService>();
            windowsService = App.GetService<IWindowsProviderService>();
        }

        public virtual void OnNavigatedTo()
        {
            ResetViewModel();
            Growl.ClearGlobal();
            App.ViewModel.IsDirty = false;
        }

        public virtual void OnNavigatedFrom()
        {
        }

        public virtual void ResetViewModel()
        {
        }

        public virtual void Navigate(Type pageType)
        {
            App.ViewModel.CurrentPageOrWinType = pageType;
            if (typeof(System.Windows.Window).IsAssignableFrom(pageType))
            {
                windowsService.ShowDialog(pageType);
            }
            else
            {
                navigationService.Navigate(pageType);
            }
        }

        [RelayCommand]
        public virtual void Back()
        {
            Type currentPageOrWinType = App.ViewModel.CurrentPageOrWinType;
            if (currentPageOrWinType != null && typeof(System.Windows.Window).IsAssignableFrom(currentPageOrWinType))
            {
                windowsService.Close(currentPageOrWinType);
                return;
            }
            App.ViewModel.CanBack = true;
            App.ViewModel.IsDirty = false;
            navigationService.GoBack();
        }

        public void NotifyChanged(string propertyName = "Item")
        {
            OnPropertyChanged(propertyName);
        }

        public virtual void ShowForm<T>() where T : class
        {
            windowsService.Show<T>();
        }

        public virtual void ShowForm(Type win)
        {
            windowsService.Show(win);
        }

        public virtual void ShowDialog<T>() where T : class
        {
            windowsService.ShowDialog<T>();
        }

        public virtual void ShowDialog(Type win)
        {
            windowsService.ShowDialog(win);
        }

        public virtual void CloseWin<T>() where T : class
        {
            windowsService.Close<T>();
        }

        public virtual void CloseWin(Type win)
        {
            windowsService.Close(win);
        }

        public virtual void HideWin<T>() where T : class
        {
            windowsService.Hide<T>();
        }

        public virtual void HideWin(Type win)
        {
            windowsService.Hide(win);
        }
    }

}
